package com.lsa.helpers.tree.binary;

import com.lsa.helpers.tree.ITreeNode;
import com.lsa.helpers.tree.ITreeVisitor;
import com.lsa.helpers.tree.TreeWalker;

public abstract class BinaryTreeWalker<T> extends TreeWalker<T>
{
    public BinaryTreeWalker(ITreeVisitor<T> visitor)
    {
        super(visitor);
    }

    public Object walk(ITreeNode<T> node) throws Exception{
    	if (node == null) return null; // empty tree
        if (node.getChildren() != null && node.getChildren().size() > 2){
            throw new IllegalArgumentException("Not a binary tree");
        }
        
        walkInOrder((IBinaryTreeNode<T>)node);
        return null;
    }
    
    protected abstract void walkInOrder(IBinaryTreeNode<T> node) throws Exception;
    
    protected void walkSubtree(IBinaryTreeNode<T> node, boolean isRight) throws Exception{
    	IBinaryTreeNode<T> child = isRight? node.getRightSubtree() : node.getLeftSubtree();
    	if (child != null){
    		walk(child);
    	}
    }
}
